package com.duing.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpConUtil {
    public static String serverUrl = "https://www.qidian.com/chapter/1044482577/838665575/";

    public static void main(String[] args) {
        String res = doGet(serverUrl);
        System.out.println("????");
    }

    public static String doGet(String str){
        HttpURLConnection conn = null;
        InputStream in = null;
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();

        try {
            URL url = new URL(str);
            conn = (HttpURLConnection) url.openConnection();

            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setRequestProperty("Accept", "application/json");

            conn.connect();

            if (conn.getResponseCode() == 200) {
                in = conn.getInputStream();
                br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
            } else {
                System.out.println("ResponseCode is not 200, is: " + conn.getResponseCode());
            }
        } catch (Exception e) {
            System.out.println("error" + e.getMessage());
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (br != null) {
                    br.close();
                }
                if (conn != null) {
                    conn.disconnect();
                }
            }catch (Exception e) {
                System.out.println("error" + e.getMessage());
            }
        }
        return sb.toString();
    }
}
